﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for TinTucProvider
/// </summary>
namespace EHTWebsite.Code
{
    public class TinTucProvider
    {
        /// <summary>
        /// ham lay tat ca tin tuc
        /// </summary>
        /// <returns></returns>
        public static DataSet GetTinTucs()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucs";
            command.CommandType = CommandType.StoredProcedure;
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }


        /// <summary>
        /// ham lay tin tuc theo tintucID
        /// </summary>
        /// <param name="TinTucID"></param>
        /// <returns></returns>
        public static TinTuc GetTinTucByTinTucID(Int32 TinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            TinTuc theTinTuc = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theTinTuc = new TinTuc();
                theTinTuc.TinTucID = Int32.Parse(ds.Tables[0].Rows[0]["TinTucID"].ToString());
                theTinTuc.TieuDeVN = ds.Tables[0].Rows[0]["TieuDeVN"].ToString();
                theTinTuc.TieuDeOther = ds.Tables[0].Rows[0]["TieuDeOther"].ToString();
                theTinTuc.NoiDungVN = ds.Tables[0].Rows[0]["NoiDungVN"].ToString();
                theTinTuc.NoiDungOther = ds.Tables[0].Rows[0]["NoiDungOther"].ToString();
                theTinTuc.NgayTao = DateTime.Parse(ds.Tables[0].Rows[0]["NgayTao"].ToString());               
                theTinTuc.TomTatVN = ds.Tables[0].Rows[0]["TomTatVN"].ToString();
                theTinTuc.TomTatOther = ds.Tables[0].Rows[0]["TomTatOther"].ToString();
                theTinTuc.HinhAnhUrl = ds.Tables[0].Rows[0]["HinhAnhUrl"].ToString();
                theTinTuc.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theTinTuc.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucTinTucID"].ToString());
                theTinTuc.NoiBat = Boolean.Parse(ds.Tables[0].Rows[0]["NoiBat"].ToString());                 
            }

            return theTinTuc;
        }

        /// <summary>
        /// ham tim kiem tin tuc theo tieu de
        /// </summary>
        /// <param name="theTinTuc"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetTinTucsByFillter(TinTuc theTinTuc ,Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucsByFillter";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TieuDeVN", theTinTuc.TieuDeVN));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", theTinTuc.TieuDeOther));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theTinTuc.DanhMucTinTucID));            
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }
        public static DataSet GetTinTucsByFillterT(TinTuc theTinTuc, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucsByFillterT";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TieuDeVN", theTinTuc.TieuDeVN));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", theTinTuc.TieuDeOther));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theTinTuc.DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }

       /// <summary>
       /// ham lay tin tuc theo danh muc tin tuc
        /// IsSelect = 1: lay tin tuc tru tin dau tien, IsSelect = 0: lay tin tuc dau tien
        /// Neu IsSelect = 1: + PageSize = 1: lay 5 tin , PageSize = 0 : lay 20 tin
        /// 
       /// </summary>
       /// <param name="DanhMucID"></param>
       /// <param name="IsSelect"></param>
       /// <returns></returns>
        public static DataSet GetTinTucsByDanhMucID(Int32 DanhMucID, Boolean IsSelect, Boolean PageSize)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetTinTucsByDanhMucID]";

            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucID));
            command.Parameters.Add(new SqlParameter("@IsSelect", IsSelect));
            command.Parameters.Add(new SqlParameter("@PageSize", PageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        /// <summary>
        /// ham lay tin tuc theo loai tin
        /// </summary>
        /// <param name="LoaiTin"></param>
        /// <returns></returns>
        public static DataSet GetTinTucByLoaiTin(Int32 LoaiTin, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetTinTucByLoaiTin]";
           
            command.CommandType = CommandType.StoredProcedure;           
            command.Parameters.Add(new SqlParameter("@LoaiTin", LoaiTin));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;          
        }

        /// <summary>
        /// ham insert tin tuc vao database
        /// </summary>
        /// <param name="theTinTuc"></param>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static Int32 IUTinTucs(TinTuc theTinTuc, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUTinTuc";
            command.CommandType = CommandType.StoredProcedure;


            SqlParameter theDanhMucMenusParameter = new SqlParameter();

            theDanhMucMenusParameter.ParameterName = "@TinTucIDOut";
            theDanhMucMenusParameter.Direction = ParameterDirection.Output;
            theDanhMucMenusParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucMenusParameter);

            command.Parameters.Add(new SqlParameter("@TinTucID", theTinTuc.TinTucID));
            command.Parameters.Add(new SqlParameter("@TieuDeVN", theTinTuc.TieuDeVN));
            command.Parameters.Add(new SqlParameter("@TieuDeOther", theTinTuc.TieuDeOther));
            command.Parameters.Add(new SqlParameter("@NoiDungVN", theTinTuc.NoiDungVN));
            command.Parameters.Add(new SqlParameter("@NoiDungOther", theTinTuc.NoiDungOther));           
            command.Parameters.Add(new SqlParameter("@TomTatVN", theTinTuc.TomTatVN));
            command.Parameters.Add(new SqlParameter("@TomTatOther", theTinTuc.TomTatOther));
            command.Parameters.Add(new SqlParameter("@HinhAnhUrl", theTinTuc.HinhAnhUrl));
            command.Parameters.Add(new SqlParameter("@SapXep", theTinTuc.SapXep));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theTinTuc.DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@NoiBat", theTinTuc.NoiBat));
            command.Parameters.Add(new SqlParameter("@LoaiTin", theTinTuc.LoaiTin)); 
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));
            DataProvider.ExecuteNoneQuery(command);
            return Int32.Parse(command.Parameters["@TinTucIDOut"].Value.ToString());
        
        }

        /// <summary>
        /// ham xoa tin tuc theo tintucID
        /// </summary>
        /// <param name="TinTucID"></param>
        public static void DeleteTinTucByTinTucID(Int32 TinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteTinTucByTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham thay doi trang thai cua tin tuc
        /// </summary>
        /// <param name="TinTucID"></param>
        /// <param name="Publish"></param>
        public static void ChangePublish(Int32 TinTucID, Boolean Publish)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishByTinTucID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucID", TinTucID));
            command.Parameters.Add(new SqlParameter("@Trangthai", Publish));
            DataProvider.ExecuteNoneQuery(command);
        }
        
        /// <summary>
        ///  ham update tin tuc thanh tin hot
        /// </summary>
        /// <param name="TinTucID"></param>
        /// <param name="DanhMucTinTucID"></param>
        /// <param name="NoiBat"></param>
        public static void UpdateTinTucHot(Int32 TinTucID, Int32 DanhMucTinTucID, Boolean NoiBat)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UTinTucHot";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TinTucHotID", TinTucID));
            command.Parameters.Add(new SqlParameter("@NoiBat", NoiBat));
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham lay tin tuc theo loai tin
        /// IsSelect = true: lay tin tucmoi nhat
        /// IsSelect = false: lay tin tuc tru tin tuc moi nhat 
        /// </summary>
        /// <param name="IsSelect"></param>
        /// <param name="LoaiTin"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetTopTinTucByLoaiTin(Boolean IsSelect, Int32 LoaiTin, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {          
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTopTinTucsByLoaiTin";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@IsSelect", IsSelect));
            command.Parameters.Add(new SqlParameter("@LoaiTin", LoaiTin));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }


        /// <summary>
        /// ham lay tin tuc moi nhat
        /// </summary>
        /// <returns></returns>
        public static DataSet GetTopNews()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTopNews";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }
    }
}